package com.zq.assistant.ui.widget.recyclerview.drag;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

/**拖动交换位置callback
 * Created by zhangqiang on 17-3-23.
 */

public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {

    private ItemTouchHelperAdapter itemTouchHelperAdapter;

    public void setItemTouchHelperAdapter(ItemTouchHelperAdapter itemTouchHelperAdapter) {
        this.itemTouchHelperAdapter = itemTouchHelperAdapter;
    }

    @Override
    public int getMovementFlags(RecyclerView mRecyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags, swipeFlags;
        if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {

            if(itemTouchHelperAdapter != null && itemTouchHelperAdapter.canItemDrag(viewHolder)){
                dragFlags = ItemTouchHelper.UP |
                        ItemTouchHelper.DOWN |
                        ItemTouchHelper.LEFT |
                        ItemTouchHelper.RIGHT;
            }else{
                dragFlags = 0;
            }
            swipeFlags = 0;
            return makeMovementFlags(dragFlags, swipeFlags);
        } else {

            if(itemTouchHelperAdapter != null && itemTouchHelperAdapter.canItemDrag(viewHolder)){
                dragFlags = ItemTouchHelper.UP |
                        ItemTouchHelper.DOWN;
            }else{
                dragFlags = 0;
            }
            swipeFlags = 0;
            return makeMovementFlags(dragFlags, swipeFlags);
        }
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {

        if(itemTouchHelperAdapter != null){

            if(itemTouchHelperAdapter.canItemDrag(target)){
                itemTouchHelperAdapter.onItemMove(viewHolder, target);
                return true;
            }
        }
        return false;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

        if(itemTouchHelperAdapter != null){
            itemTouchHelperAdapter.onItemDismiss(viewHolder);
        }
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE && itemTouchHelperAdapter != null) {
            //当滑动或者拖拽view的时候通过接口返回该ViewHolder
            itemTouchHelperAdapter.onItemSelect(viewHolder);
        }
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        if (!recyclerView.isComputingLayout() && itemTouchHelperAdapter != null) {
            //当需要清除之前在onSelectedChanged或者onChildDraw,onChildDrawOver设置的状态或者动画时通过接口返回该ViewHolder
            itemTouchHelperAdapter.onItemClear(viewHolder);
        }
    }

}
